﻿namespace ACCOUNTING
{
    using C1.Win.C1FlexGrid;
    using Microsoft.VisualBasic;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Data;
    using System.Diagnostics;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;

    public class FilterRow
    {
        private int _col = -1;
        private C1.Win.C1FlexGrid.C1FlexGrid _flex;
        private int _row = -1;
        private CellStyle _style;

        public FilterRow(C1.Win.C1FlexGrid.C1FlexGrid flex)
        {
            this._flex = flex;
            this._row = this._flex.Rows.Fixed;
            this._flex.Rows.Fixed++;
            this._style = this._flex.Styles.Add("Filter", this._flex.Styles.Frozen);
            this._style.BackColor = SystemColors.Info;
            this._style.ForeColor = SystemColors.InfoText;
            this._style.Border.Direction = BorderDirEnum.Horizontal;
            this._flex.Rows[this._row].Style = this._style;
            this._flex.KeyDown += new KeyEventHandler(this._flex_KeyDown);
            this._flex.BeforeMouseDown += new BeforeMouseDownEventHandler(this._flex_BeforeMouseDown);
            this._flex.RowColChange += new EventHandler(this._flex_RowColChange);
            this._flex.AfterEdit += new RowColEventHandler(this._flex_AfterEdit);
            this._flex.Select(this._row, this._flex.Cols.Fixed);
        }

        private void _flex_AfterEdit(object sender, RowColEventArgs e)
        {
            if (e.Row == this._row)
            {
                this.UpdateFilter();
            }
        }

        private void _flex_BeforeMouseDown(object sender, BeforeMouseDownEventArgs e)
        {
            if ((((e.Button & MouseButtons.Left) != MouseButtons.None) && (this._row > 0)) && (this._flex.MouseRow == this._row))
            {
                int mouseCol = this._flex.MouseCol;
                if (mouseCol < this._flex.Cols.Fixed)
                {
                    this._flex.Select(this._row, this._flex.Cols.Fixed, this._row, this._flex.Cols.Count - 1);
                    this._flex.FinishEditing(true);
                }
                else
                {
                    this._flex.Select(this._row, mouseCol);
                }
                e.Cancel = true;
            }
        }

        private void _flex_KeyDown(object sender, KeyEventArgs e)
        {
            if ((e.KeyCode == Keys.F3) || ((e.KeyCode == Keys.Up) && (this._flex.Row == (this._row + 1))))
            {
                this._flex.Select(this._row, this._flex.Col);
                e.Handled = true;
            }
            if ((e.KeyCode == Keys.Delete) && (this._flex.Row == this._row))
            {
                int num2 = this._flex.Cols.Count - 1;
                for (int i = this._flex.Cols.Fixed; i <= num2; i++)
                {
                    this._flex[this._row, i] = null;
                }
                this.UpdateFilter();
                e.Handled = true;
            }
        }

        private void _flex_RowColChange(object sender, EventArgs e)
        {
            if ((this._row > -1) && ((this._flex.Row != this._row) || (this._flex.Col != this._col)))
            {
                this._col = -1;
                if (this._flex.Row == this._row)
                {
                    this._col = this._flex.Col;
                    this._flex.StartEditing();
                }
            }
        }

        private string BuildFilterExpression(int col, string expr)
        {
            string str2 = "<>=";
            if (str2.IndexOf(Strings.Left(expr, 1)) < 0)
            {
                if (this._flex.Cols[col].DataType == typeof(string))
                {
                    return string.Format(" like '{0}*'", expr);
                }
                return string.Format(" = '{0}'", expr);
            }
            int num2 = expr.Length - 1;
            for (int i = 0; i <= num2; i++)
            {
                if (str2.IndexOf(Strings.Mid(expr, i + 1, 1)) < 0)
                {
                    return (expr.Substring(0, i) + " " + string.Format("'{0}'", expr.Substring(i).Trim()));
                }
            }
            Debug.WriteLine("Can't build filter expression.");
            return "";
        }

        public void Clear()
        {
            int num2 = this._flex.Cols.Count - 1;
            for (int i = 0; i <= num2; i++)
            {
                this._flex[this._row, i] = null;
            }
            this.UpdateFilter();
        }

        private void UpdateFilter()
        {
            if (this._row >= 0)
            {
                DataView dataSource = null;
                if (this._flex.DataSource is DataView)
                {
                    dataSource = (DataView) this._flex.DataSource;
                }
                if (this._flex.DataSource is DataTable)
                {
                    DataTable table = (DataTable) this._flex.DataSource;
                    dataSource = table.DefaultView;
                }
                if (dataSource == null)
                {
                    Debug.WriteLine("DataSource should be a DataTable or DataView.");
                }
                else
                {
                    StringBuilder builder = new StringBuilder();
                    int num2 = this._flex.Cols.Count - 1;
                    for (int i = this._flex.Cols.Fixed; i <= num2; i++)
                    {
                        string expr = this._flex.GetDataDisplay(this._row, i).TrimEnd(new char[0]);
                        if (expr.Length > 0)
                        {
                            expr = this.BuildFilterExpression(i, expr);
                            if (expr.Length > 0)
                            {
                                if (builder.Length > 0)
                                {
                                    builder.Append(" And ");
                                }
                                builder.AppendFormat("[{0}]{1}", this._flex.Cols[i].Name, expr);
                            }
                        }
                    }
                    string str = builder.ToString();
                    if (str != dataSource.RowFilter)
                    {
                        try
                        {
                            this._flex[this._row, 0] = null;
                            dataSource.RowFilter = str;
                        }
                        catch (Exception exception1)
                        {
                            ProjectData.SetProjectError(exception1);
                            this._flex[this._row, 0] = "Err";
                            Debug.WriteLine("Bad filter expression.");
                            ProjectData.ClearProjectError();
                        }
                        this._flex.Row = this._row;
                    }
                }
            }
        }

        public bool Visible
        {
            get
            {
                return this._flex.Rows[this._row].Visible;
            }
            set
            {
                this._flex.Rows[this._row].Visible = value;
            }
        }
    }
}

